import traceback
from flask import make_response
from json import dumps
from ..libs.jsonify import CustomJSONEncoder

def api_return(code = 200, msg = '', data = []):
    return {
        'code': code,
        'msg': msg,
        'response': data
    }


def output_json(data, code, headers=None):
    try:
        print('### 返回值 ###',data)
        # 此处为自定义添加***************
        resource = ''
        if 'message' not in data:
            resource = data.get('resource')
            msg = 'success'
        else:
            msg = data.get('message')
            if type(msg) != str:
                for i in msg.values():
                    msg = i
        data = {'code': code, 'message': msg,'resource': resource}
        # print(data)
    except Exception as e:
        print('### 返回值封装报错 ###')
        print("traceback:", traceback.format_exc())
    # 系统输出
    resp = make_response(dumps(data), code)
    resp.headers.extend(headers or {})
    return resp
